//
//  WalkVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/24.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "WalkVC.h"
#import "RankingVC.h"
#import "HttpTool.h"
#import "WalkBase.h"
#import "MJExtension.h"
#import "WalkToday.h"
#import "WalkStepBase.h"
#import "MJRefresh.h"
#import "RunCell.h"
#import "Walks.h"
@interface WalkVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *sportData;
    PageData *page;
}
@property (weak, nonatomic) IBOutlet UILabel *step;
- (IBAction)RangKing:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *rangk;
@property (weak, nonatomic) IBOutlet UILabel *distance;
@property (weak, nonatomic) IBOutlet UILabel *cal;//热量
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation WalkVC

- (void)viewDidLoad {
    [super viewDidLoad];
    page =[[PageData alloc]init];
    sportData=[NSMutableArray array];
    _table.delegate =self;
    [self getStepToday];
    [self RefreshSport];
}
- (IBAction)RangKing:(id)sender {
    RankingVC *rang =[[RankingVC alloc]init];
    [self.navigationController pushViewController:rang animated:YES];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RunCell *cell =  [RunCell setRunCellWithTableView:tableView];
    WalkSteps *steps = sportData[indexPath.row];
    cell.steps =steps;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return sportData.count;
}
-(void)RefreshSport{
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    __unsafe_unretained __typeof(self) weakSelf = self;
    _table.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{//下拉刷新
        [weakSelf updateSports];
    }];
    [_table.mj_header beginRefreshing];
    
    _table.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreSports];
    }];
}

-(void)loadMoreSports{
     page.page =page.page+1;
     NSNumber *pageNumber = [NSNumber numberWithInt:page.page];
    NSDictionary *params = @{@"user_id":@"phone13480151236",@"page":pageNumber,@"pageSize":@10};
    [HttpTool GET:@"http://www.aiaiaini.com/demo/web/index.php?r=api/step/get-walks" WithHeader:NO parameters:params success:^(id responseObject) {
        WalkStepBase *data =[WalkStepBase mj_objectWithKeyValues:responseObject];
        if (data.status) {
            //设置数据
            Walks *walk =data.data;
            NSArray *walks = walk.walks;
            if (walks.count!=0) {
                [sportData addObjectsFromArray:walks];
            }
            [_table reloadData];
        }
        [_table.mj_footer endRefreshing];
    } failure:^(NSError *error){
        [_table.mj_footer endRefreshing];
        NSLog(@"%@",error);
    }];
}
-(void)updateSports{
     page.page =1;
    NSDictionary *params = @{@"user_id":@"phone13480151236",@"page":@1,@"pageSize":@10};
    [HttpTool GET:@"http://www.aiaiaini.com/demo/web/index.php?r=api/step/get-walks" WithHeader:NO parameters:params success:^(id responseObject) {
        WalkStepBase *data =[WalkStepBase mj_objectWithKeyValues:responseObject];
        if (data.status){
            //设置数据
            Walks *walk =data.data;
            NSArray *walks = walk.walks;
            if (sportData.count!=0) {
                [sportData removeAllObjects];
            }
            [sportData addObjectsFromArray:walks];
            [_table reloadData];
        }
         [_table.mj_header endRefreshing];
    } failure:^(NSError *error){
         [_table.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}
-(void)getStepToday{//只需要一id
    NSDictionary *params = @{@"user_id":@"phone13480151236"};
    [HttpTool GET:@"http://www.aiaiaini.com/demo/web/index.php?r=api/step/get-today" WithHeader:NO parameters:params success:^(id responseObject) {
        WalkBase *data =[WalkBase mj_objectWithKeyValues:responseObject];
        if (data.status) {
            NSLog(@"getStepToday");
            WalkToday *today =data.data;
            _step.text =[NSString stringWithFormat:@"%i",today.step];
            _distance.text = [NSString stringWithFormat:@"%i",today.distance/1000];
            _cal.text=[NSString stringWithFormat:@"%i",today.calorie/1000];
            _rangk.text=[NSString stringWithFormat:@"%i",today.rank];
        }else{
        
        }
    } failure:^(NSError *error){
        NSLog(@"%@",error);
    }];
}
@end
